#ifndef nutritionpanel_h
#define nutritionpanel_h

#include <iostream>
#include "juce_amalgamated.h"
#include "BoundedPanel.h"
#include "SwitchablePanel.h"
#include "BackButton.h"

class NutritionGuidePanel: public BoundedPanel, public SwitchablePanel {
private:
	String textLeft;
	String textRight;
	TextEditor editor;
	BackButton* backButton;
	TextLayout* textLayoutLeft;
	TextLayout* textLayoutRight;
	Font* titleFont;
	Font* headingFont;
	Image* foodPyramid;

	void readText() {
		File fileLeft(T("nutritionleft.txt"));
		if (!fileLeft.exists()) {
			std::cerr << "Could not find left nutrition file" << std::endl;
			return;
		}
		textLeft = fileLeft.loadFileAsString();

		File fileRight(T("nutritionright.txt"));
		if(!fileRight.exists()) {
			std::cerr << "Could not find right nutrition file" << std::endl;
			return;
		}
		textRight = fileRight.loadFileAsString();
	}

	void loadImage() {
		File file(T("images/food_pyramid.jpg"));
		foodPyramid = juce::JPEGImageFormat().loadFrom(file);
		if (!foodPyramid) {
			std::cout << "Could not load food pyramid file" << std::endl;
			return;
		}
	}

public:
	NutritionGuidePanel(MasterComponent* master, int w, int h) :
		BoundedPanel(w, h), SwitchablePanel(master) {
		readText();
		loadImage();
		textLayoutLeft = new TextLayout(textLeft, Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
		textLayoutLeft->layout(width / 2 - 100, Justification::left, true);
		textLayoutRight = new TextLayout(textRight, Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
		textLayoutRight->layout(width / 2 - 100, Justification::left, false);

		titleFont = new Font(Font::getDefaultSansSerifFontName(), 36, Font::plain);
		headingFont = new Font(Font::getDefaultSansSerifFontName(), 18, Font::bold);

		addAndMakeVisible(backButton = new BackButton((SwitchablePanel*) this,
				MasterComponent::NUTRITION, MasterComponent::MAIN));
		backButton->setBounds(35, 35, 40, 40);
	}

	~NutritionGuidePanel() {
		delete textLayoutLeft;
		delete textLayoutRight;
		delete titleFont;
		delete headingFont;
		delete foodPyramid;
		delete backButton;
	}

	void paint(Graphics& g) {
		Rectangle r(0, 0, width, height);

		g.setColour(Colour(230, 240, 250));
		g.fillRoundedRectangle(r, 60);
		g.setColour(Colours::black);

		g.setFont(*titleFont);
//		g.drawSingleLineText(T("Nutrition"), 80, 100);
		g.drawFittedText(T("Nutrition"), width / 2 - 100, 0, 200, 100, Justification::centred, 1);

		int textStart = 100;
		int textEnd = textLayoutLeft->getHeight() + textStart;
		textLayoutLeft->draw(g, 85, textStart);
		textLayoutRight->draw(g, width / 2 + 40, textStart);

		g.setColour(Colours::grey);
		g.setColour(Colours::lightblue);
		MiscGraphics::drawRoundedRect(g, r, 60, 1);

		g.drawImageAt(foodPyramid, 85, textEnd + 50);
		g.drawRect(85, textEnd + 50, foodPyramid->getWidth(), foodPyramid->getHeight());
	}

	void mouseDown(const MouseEvent& e) {

	}

	void switchPanels(int from, int to) {
		master->switchPanels(from, to);
	}

	void resized() {

	}
};

#endif
